// 初始化数据
import { Order } from '../vendor/order_food/order';
import { Goods } from '../vendor/order_food/goods';
import { Category } from '../vendor/order_food/category';

import getOrders from '../vendor/order_food/order';
import getGoods from '../vendor/order_food/goods';
import getCategory from '../vendor/order_food/category';

const reqTime = 1000;




export type State = {
	/// 商户端缓存策略:
	//  首屏加载: 
	// 1.缓存所属商户商品列表
	// 2.缓存订单信息
	// 3.缓存用户信息(昵称、头像)
	// 4.缓存用户收货地址 
	// 5.缓存商家端店铺信息
	
	
	
	/// 用户端缓存策略: 
	//  首屏加载: 
	// 1.缓存所属商户商品列表
	// 2.缓存订单信息
	// 3.缓存用户信息(昵称、头像等)
	// 4.缓存用户收货地址 
	// 5.缓存商家端店铺信息
	// 需要定义：用户端->用户侧看到的商户端信息  商户端->商户侧看到的商户端信息
	// 用户端
	userGoodsList:Array<Goods> //  
	
	busGoodsList: Array<Goods>;
	busOrderList: Array<Order>;   
	orderPage: number;

	
	
	goodsPage: number;
	busCategoryList: Array<Category>;
	categoryPage: number;
	userOrderPage:number
	userOrderList: Array<Order>;
};

export const state = { busOrderList: [], orderPage: 1, goodsPage: 1, categoryPage: 1, userOrderPage:1, busGoodsList: [], busCategoryList: [] } as State;

export const setUserOrderPage = (page: number) => {
	state.userOrderPage = page;
};

export const setOrderPage = (page: number) => {
	state.orderPage = page;
};

export const setGoodsPage = (page: number) => {
	state.goodsPage = page;
};

export const setCategoryPage = (page: number) => {
	state.categoryPage = page;
};

export const setBusOrderList = (busOrderList: Array<Order>) => {
	state.busOrderList = busOrderList;
};

export const setBusGoodsList = (busGoodsList: Array<Goods>) => {
	state.busGoodsList = busGoodsList;
};

export const setBusCategoryList = (busCategoryList: Array<Category>) => {
	state.busCategoryList = busCategoryList;
};

export const setUserOrderList = (userOrderList: Array<Order>) => {
	state.userOrderList = userOrderList;
};

//////////// 商户

// 订单列表
export const busOrderList = () => {
	const { list, getList } = getOrders();
	const f = () => {
		getList(state.orderPage, (res: any) => {
			
			setBusOrderList(list.value)
			
			if (res.list && res.list.length) {
				// 继续请求
				setTimeout(() => {
					setOrderPage(state.orderPage + 1);
					f();
				}, reqTime);
			}
			
		});
	};
};

// 商品列表
export const busGoodsList = () => {
	const { list, getList } = getGoods();
	getList(0,()=>{
		setBusGoodsList(list.value)
	});
};


// 分类列表
export const busCategoryList = () => {
	const { list, getList } = getCategory();
	getList(()=>{
		setBusCategoryList(list.value)
	});
};


////////// 用户

// 订单列表
export const userOrderList = () => {
	const { list, getList } = getOrders();
	const f = () => {
		getList(state.userOrderPage, (res: any) => {
			
			setUserOrderList(list.value)
			
			if (res.list && res.list.length) {
				// 继续请求
				setTimeout(() => {
					setUserOrderPage(state.userOrderPage + 1);
					f();
				}, reqTime);
			}
			
		});
	};
};